package com.karaoke.android.kege.vm

import android.databinding.ObservableArrayList
import android.databinding.ObservableBoolean
import android.databinding.ObservableList
import android.databinding.adapters.ViewBindingAdapter
import android.os.Bundle
import android.view.View
import com.gturedi.views.StatefulLayout
import com.karaoke.android.data.LoadMoreEvent
import com.karaoke.android.data.NetworkTools
import com.karaoke.android.data.domain.QSinger
import com.karaoke.android.data.domain.Singer
import com.karaoke.android.data.onIO
import com.karaoke.android.data.onUI
import com.karaoke.android.kege.BR
import com.karaoke.android.kege.OnSearchTextChangedEvent
import com.karaoke.android.kege.R
import com.karaoke.android.kege.SearchTypeChangedEvent
import com.karaoke.android.kege.adapter.SingerGridAdapter
import com.karaoke.android.library.databind.ItemBinding
import com.karaoke.android.library.mvvm.FragmentViewModel
import com.trello.rxlifecycle.kotlin.bindToLifecycle
import org.simple.eventbus.EventBus
import org.simple.eventbus.Subscriber
import rx.Subscription

/**
 * Created by allen on 2017/4/20.
 */

class SingerSearchViewModel : FragmentViewModel()
{
    private val qSinger: QSinger = QSinger()
    private var state: StatefulLayout? = null

    val canLoadMore = ObservableBoolean()
    val singers: ObservableList<Singer> = ObservableArrayList<Singer>()
    val singerBindings = ItemBinding.of<Singer>(BR.item, R.layout.singer_item)
    val adapter = SingerGridAdapter<Singer>()
    val loading = ObservableBoolean()

    init
    {
        EventBus.getDefault().register(this)
    }

    override fun onDestroy()
    {
        EventBus.getDefault().unregister(this)
        super.onDestroy()
    }

    override fun onInitLoad()
    {
        super.onInitLoad()
        EventBus.getDefault().post(SearchTypeChangedEvent(qSinger.searchText))
        state?.showEmpty("输入歌星首字母或名称")
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?)
    {
        super.onViewCreated(view, savedInstanceState)
    }

    @Subscriber
    fun onMoreAsked(e: LoadMoreEvent)
    {
        if (!loading.get() && canLoadMore.get())
        {
            loading.set(true)
            qSinger.page += 1
            NetworkTools.singerService.getSingers(qSinger).bindToLifecycle(this).onIO().onUI {
                singers.addAll(it.list)
                canLoadMore.set(it.hasNextPage)
                loading.set(false)
            }
        }
    }

    val onViewAttachedToWindow = ViewBindingAdapter.OnViewAttachedToWindow {
        if (it.id == R.id.stateful)
            state = it as StatefulLayout?
    }

    private var searchSingerSubscription: Subscription? = null

    @Subscriber
    fun onSearch(e: OnSearchTextChangedEvent)
    {
        qSinger.searchText = e.text
        singers.clear()
        canLoadMore.set(false)
        if (!e.text.isNullOrBlank())
        {
            state?.showLoading("搜索中...")
            searchSingerSubscription?.unsubscribe()
            searchSingerSubscription = NetworkTools.singerService.getSingers(qSinger).bindToLifecycle(this).onIO().onUI {
                singers.addAll(it.list)
                canLoadMore.set(it.hasNextPage)
                if (it.list.isEmpty())
                    state?.showEmpty("没有搜索到歌星")
                else
                    state?.showContent()
            }
        }
    }
}
